// 导出请求函数
export default function request(options) {
	// 后端接口地址
	const baseUrl = 'http://localhost:8080';
	// const baseUrl = 'http://localhost:8800';
	const token = uni.getStorageSync("token")

	const {
		method,
		url,
		data,
		header
	} = options

	// 合并自定义header和默认header
	// 修改后
	const defaultHeader = {
	    "Authorization": token ? `Bearer ${token}` : '',
	    "Content-Type": "application/json"
	};

	const mergedHeader = header ? {
		...defaultHeader,
		...header
	} : defaultHeader;

	return new Promise((resolve, reject) => {
		uni.request({
			method: method,
			url: baseUrl + url,
			data: data,
			header: mergedHeader,
		}).then((response) => {
			let [error, res] = response;
			if (error) {
				reject(error);
			} else {
				resolve(res.data);
			}
		}).catch(error => {
			reject(error);
		})
	})
}